package cbir;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.*;


public class md5 {

	public static String calcola(File f) throws NoSuchAlgorithmException, IOException{
		
		// First, we need to get an implementation of java.security.MessageDigest
		MessageDigest digest = MessageDigest.getInstance("MD5");

		
		InputStream is = new FileInputStream(f);
		byte[] buffer = new byte[8192];
		int read = 0;
		while( (read = is.read(buffer)) > 0) {
			digest.update(buffer, 0, read);
		}
		byte[] md5sum = digest.digest();
		BigInteger bigInt = new BigInteger(1, md5sum);
		String output = bigInt.toString(16);
		is.close();
	
		return output;
	}
	
	public static void main(String[] args) {
		File f = new File("/home/math/a.jpg");
		try {
			System.out.println(calcola(f));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
